import { h } from 'vue';
import { NTag, NTooltip } from 'naive-ui';
import { BasicColumn } from '@/components/Table';
import { BillingRecord } from '@/api/billing';
import { initial } from 'lodash';

// 状态映射
const statusMap = {
  done: { text: '成功', type: 'success' as const },
  fail: { text: '失败', type: 'error' as const },
  init: { text: '处理中', type: 'warning' as const },
};

// 类型映射
const typeMap = {
  RECHARGE: '充值',
  REFUND: '退款',
  TRANSFER: '转账',

};

// 退款类型映射
const refundTypeMap = {
  full: '全额退款',
  part: '部分退款',
};

export const columns: BasicColumn<BillingRecord>[] = [
  // {
  //   title: 'ID',
  //   key: 'id',
  //   width: 80,
  //   // fixed: 'left',
  // },
  {
    title: '账户ID',
    key: 'account_id',
    width: 140,
    ellipsis: {
      tooltip: true,
    },
  },
  {
    title: '类型',
    key: 'type',
    width: 60,
    render(record) {
      const typeText = typeMap[record.type] || record.type;
      const isRefund = record.type === 'REFUND';

      return h(
        'span',
        {
          style: {
            color: isRefund ? '#d03050' : undefined,
          },
        },
        typeText
      );
    },
  },
  {
    title: '账户币',
    key: 'amount',
    width: 90,
    render(record) {
      return `${parseFloat(record.amount).toFixed(2)}`;
    },
  },
  {
    title: '实际金额',
    key: 'actual_amount',
    width: 90,
    render(record) {
      return `${parseFloat(record.actual_amount).toFixed(2)}`;
    },
  },
  {
    title: '状态',
    key: 'status',
    width: 80,
    render(record) {
      const status = statusMap[record.status] || { text: record.status, type: 'default' as const };
      return h(NTag, { type: status.type }, { default: () => status.text });
    },
  },
  // {
  //   title: '退款类型',
  //   key: 'refund_type',
  //   width: 100,
  //   render(record) {
  //     if (!record.refund_type) return '-';
  //     return refundTypeMap[record.refund_type] || record.refund_type;
  //   },
  // },
  // {
  //   title: '返利金额',
  //   key: 'rebate_amount',
  //   width: 120,
  //   render(record) {
  //     return `¥${parseFloat(record.rebate_amount).toFixed(2)}`;
  //   },
  // },
  // {
  //   title: '利润',
  //   key: 'profit',
  //   width: 100,
  //   render(record) {
  //     const profit = parseFloat(record.profit);
  //     return h(
  //       'span',
  //       {
  //         style: {
  //           color: profit >= 0 ? '#18a058' : '#d03050',
  //         },
  //       },
  //       `¥${profit.toFixed(2)}`
  //     );
  //   },
  // },
  // {
  //   title: '上游API',
  //   key: 'upstream_api',
  //   width: 120,
  //   ellipsis: {
  //     tooltip: true,
  //   },
  // },
  {
    title: '备注',
    key: 'remark',
    width: 140,
    ellipsis: {
      tooltip: true,
    },
    render(record) {
      if (!record.remark) return '-';

      // 失败状态的备注显示红色
      const isFailStatus = record.status === 'fail';
      const displayText = record.remark.length > 20 ? `${record.remark.slice(0, 20)}...` : record.remark;

      return h(
        NTooltip,
        {},
        {
          trigger: () => h(
            'span',
            {
              style: {
                color: isFailStatus ? '#d03050' : undefined,
              },
            },
            displayText
          ),
          default: () => record.remark,
        }
      );
    },
  },
  {
    title: '备款户ID',
    key: 'wallet_account_id',
    width: 120,
    ellipsis: {
      tooltip: true,
    },
  },
  {
    title: '创建时间',
    key: 'created_at',
    width: 140,
    // fixed: 'right',
  },
  {
    title: '更新时间',
    key: 'updated_at',
    width: 140,
    // fixed: 'right',
  },
];
